<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\controller\Door;
use app\service\lockService;
use think\Db;
use think\Log;
class Lock extends Common{
    //位置查询
    public function fetchdata() {
        $locktype = request()->post('locktype');
        $community = request()->post('community');
        if($locktype == 1){
            //查询小区信息
            return array('小区大门'); 
        }elseif($locktype == 2){
            //根据小区查询楼栋信息
            $grouplist = array_unique((new lockService())->groupList($community));
            // dump($grouplist);exit();
            $arr = [];
            foreach ($grouplist as $group) {
                //根据组团查询楼栋
                $buildinglist = array_unique((new lockService())->buildingList($community,$group));
                // dump($buildinglist);exit();
                foreach($buildinglist as $building){
                    $unitlist = array_unique((new lockService())->unitList($community,$group,$building));
                    foreach($unitlist as $unit){
                        $arr[] = $group.$building.'幢'.$unit.'单元';
                    }
                } 
            }
            return array_values(array_unique($arr));  
        }elseif($locktype == 3){
            return array('别墅小门'); 
        }
    }
	
	 //门锁管理
    public function lock(){
		
        //根据当前账号查询小区
        $com = $this->pidTokenId();
        $whe=array();
        if(!empty($com)){
            $whe['a.community_id'] = array("in",$com['community_id']);
        }

        $locklist=Db::name('lock')
                    ->alias("a")
                    ->join("community b","a.community_id=b.id")
                    ->where($whe)
                    ->field("a.*,b.name")
                    ->order('id asc')
                    ->paginate(100);


        $this->assign('list',$locklist);
        $number = 0;
        $this->assign('number',$number);
        $this->assign('locklist',$locklist);
        return $this->fetch('lock_list');
    } 
	

    //门锁管理
    public function lockList(){
        //根据当前账号查询小区
        $com = $this->pidTokenId();
        if(!empty($com)){
            $whe['community_id'] = $com['id'];
        }else{
            $whe = null;
        }
        $locklist = (new lockService())->lockList($whe);
        //var_dump($locklist);die;
        foreach($locklist as $lock){
            //根据小区编号查询小区名称
            $comid = $lock['community_id'];
            $community = (new lockService())->communityName($comid);
            $lock['community'] = $community['name'];
            //数据从新存入数组
            $list[] = $lock;
        }
        if(!isset($list)){
            $this->assign('list',$locklist);
        }else{
            $this->assign('list',$list);
        }
        $number = count($locklist);
        $this->assign('number',$number);
        $this->assign('locklist',$locklist);
        return $this->fetch('lock_list');
    } 
    //添加
    public function lockAdd(){
        //查询小区信息
        $com = $this->pidTokenId();

        $whe['id'] = array("in",$com['community_id']);
        $community = (new lockService())->communityList($whe);


        $this->assign('community',$community);

        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();

            //判断账号名称是否重复        
            $lockid = (new lockService())->lockid($data['serial']);
            if($lockid){
                $this->error('门锁已存在');
            }
            $data['status'] = 2;
            $data['time'] = time();
            //插入数据到数据库
            $result = (new lockService())->lockAdd($data);
            if($result){
                $this->success('添加门锁成功',url('lockAdd'));
            }else{
                $this->error('添加门锁失败',url('lockAdd'));
            }
        }
        return $this->fetch('lock_add');
    }
    //修改
    public function lockEdit(){
        //查询小区信息
        $com = $this->pidTokenId();
        $whe['id'] = array("in",$com['community_id']);
        $community = (new lockService())->communityList($whe);
        $this->assign('community',$community);
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('门锁不存在');
            }

            $info = Db::name('lock')->where('id',$id)->find();
            $this->assign('info',$info);
            dump($info);
            return $this->fetch('lock_edit');
        }else{
            $data = request()->post();
            $result = (new lockService())->lockEdit($data);
            if($result){
                    $this->success('修改门锁成功',url('lockList'));
            }else{
                    $this->error('修改门锁失败');
            }
        }
    }
    //删除
    public function lockDel(){
        $serial = request()->param('serial');
		$validate = $this->validate(request()->param(),[
			'serial'	=>	'require'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
		$info = (new lockService())->lockId($serial);
		if(empty($info)){
			$this->error('该门锁不存在');
		}
		$result =  (new lockService())->lockDel($serial);
        if($result){
            $this->success('删除门锁成功',url('lockList'));
		}else{
            $this->success('删除门锁失败',url('lockList'));
		}
    }
    //上线
    public function lockUpper(){
        $serial = request()->param('serial');
        //根据门锁ID查找门锁信息
        $lockinfo = (new LockService())->lockId($serial);
        $community = $lockinfo['community'];
        //根据小区ID查找小区信息
        $communityinfo = (new lockService())->communityId($community);
        $doorreturn = (new Door())->door($communityinfo['property'],$serial,1);
        if($doorreturn['state'] == 1){
            $lockcode = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2018090361235311&scope=auth_user&redirect_uri=https://www.aliandongpay.com/admin/Apiuser/apiuser?serial='.$serial;
            //修改门锁状态
            $status = Db::name('lock')->where('serial',$serial)->update(['status' => '1','lockcode' => $lockcode]);
            $this->success('门锁上线成功',url('lockList'));
        }else{
            $this->success('门锁上线失败',url('lockList'));
		}
    }
    //下线
    public function lockLower(){
        $serial = request()->param('serial');
        //根据门锁ID查找门锁信息
        $lockinfo = (new LockService())->lockId($serial);
        $community = $lockinfo['community'];
        //根据小区ID查找小区信息
        $communityinfo = (new lockService())->communityId($community);
        $doorreturn = (new Door())->door($communityinfo['property'],$serial,3);
        if($doorreturn['state'] == 1){
            //修改门锁状态
            $status = Db::name('lock')->where('serial',$serial)->update(['status' => '2']);
            $this->success('门锁下线成功',url('lockList'));
        }else{
            $this->success('门锁下线失败',url('lockList'));
		}
    }
    //开门
    public function lockOpen(){
        $serial = request()->param('serial');
        //根据门锁ID查找门锁信息
        $lockinfo = (new LockService())->lockId($serial);
        $community = $lockinfo['community'];
        //根据小区ID查找小区信息
        $communityinfo = (new lockService())->communityId($community);
        $doorreturn = (new Door())->door($communityinfo['property'],$serial,4);
		//Log::write($doorreturn,"开门结果");
        if($doorreturn['state'] == 1){
            $this->success('开门成功',url('lockList'));
        }else{
            $this->success('开门失败',url('lockList'));
		}
    }








    // //生成二维码
    // public function qrcode(){
    //     $id = request()->param('id');
    //     $url="https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2018032202429915&scope=auth_base&redirect_uri=https://www.aliyunzz.com/admin/Apiuser/apiuser?locksnid=".$id;
    //     $level=2;
    //     $size=10;
    //     Vendor('phpqrcode.phpqrcode');
    //     $errorCorrectionLevel =intval($level) ;//容错级别 
    //     $matrixPointSize = intval($size);//生成图片大小 
    //     //生成二维码图片 
    //     $object = new \QRcode();
    //     $image = $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    //     exit();
    // }
}